****Appalachian Regional Comission
clear all
cls
capture log close // Closes any open logs
version 15 // Tells STATA what version to use

*Begin Assignmment
global dir "/Users/DanielBFiroozi/Documents/Research/Appalachia Regional Commission/"
log using "$dir/log/script", replace

set matsize 11000
set maxvar 30000

est clear



*Data Section: Grant Spending Section
{
use "$dir/projects/projects_expanded.dta"
tab spendinggroup if fipscode!=. [fw= int(totalfunding)], sort
tab spendinggroup [fw= int(totalfunding)], sort
codebook fipscode
clear
}
use "$dir/readydata/REStat_Firoozi22.dta", clear

*Summary Stats
{

sum  hsgradrate bachrate urate2009 prate2009 income2009  

sum  hsgradrate bachrate urate2009 prate2009 income2009   if rindex>=90

forvalues i=2000(4)2016{
gen total2partyvotes`i'=democrat`i'+republican`i'
}

forvalues i=2000(4)2016{
sum  d_2party`i'  [aw=total2partyvotes`i']
sum  d_2party`i'  [aw=total2partyvotes`i'] if rindex>=90
}
drop total2partyvotes*

}

*McCrary Test
{
hist index2009, plotregion(color(white)) width(2) start(-120) graphregion(color(white)) bcolor(blue) ///
xtitle(Rank Index) xline(0, lcolor(red) lpattern(dash)) saving("$dir/figures/histogram",replace)
graph use "$dir/figures/histogram"

preserve
gen bw=2
gen rbin1= bw*floor(index2009/bw)+bw/2
gen count=1 
collapse (sum) count ,  by(rbin1) 

twoway  (scatter count rbin1 , msize(vsmall) msymbol(circle_hollow)) ///
		(lpoly count rbin if rbin<0 , lcolor(black)) ///
		(lpoly count rbin  if rbin>=0 , lcolor(black)) ///
		, legend(off) ///
		xtitle("Rank Index") ytitle("Count") xline(0, lcolor(red) lpattern(dash)) ///
		plotregion(color(white)) graphregion(color(white)) saving("$dir/figures/McCrary",replace)
graph use "$dir/figures/McCrary"
graph export "$dir/figures/McCrary.png",replace
restore

graph combine "$dir/figures/histogram" "$dir/figures/McCrary", ///
plotregion(color(white)) graphregion(color(white)) cols(1) xcommon
graph export "$dir/figures/McCrary_total.png",replace


}

*Balance Tests
{
*Covariate RD Graphs
{

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome hs_scaled
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Scaled High School Graduation") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome coll_scaled
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Scaled College Graduation") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome urate_scaled2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Scaled 2004-6 Unemployment") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome prate_scaled2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Scaled 2000 Poverty") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome income_scaled2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Scaled 2005 Market Income") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome hsgradrate
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2000 HS Grad Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome bachrate
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2000 College Grad Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome urate2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004-6 Unemployment Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome prate2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2000 Poverty Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve
replace income2009=income2009/1000
gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome income2009
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2005 Market Income ($1000s)") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome erate2006
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2006 Employment Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve
gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party2004
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004 John Kerry Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve

gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party2000
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004 Al Gore Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve
gen bw=0.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome dhouse_2party2004
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004 House Democrat Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore


graph combine "$dir/figures/prate2009" "$dir/figures/urate2009" ///
 "$dir/figures/income2009" "$dir/figures/prate_scaled2009" ///
 "$dir/figures/urate_scaled2009" "$dir/figures/income_scaled2009" , ///
plotregion(color(white)) graphregion(color(white)) rows(2) xsize(6) 
graph export "$dir/figures/balancechecks_graph1.png",replace


graph combine "$dir/figures/hs_scaled" "$dir/figures/coll_scaled" ///
 "$dir/figures/d_2party2004" "$dir/figures/dhouse_2party2004" "$dir/figures/hsgradrate" ///
 "$dir/figures/bachrate" "$dir/figures/d_2party2000" "$dir/figures/erate2006", ///
plotregion(color(white)) graphregion(color(white)) rows(2) xsize(6) 
graph export "$dir/figures/balancechecks_graph2.png",replace
}
*Covariate Null Effect BW Robustness Graphs
{


preserve
global outcome income_scaled
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Scaled 2005 Market Income") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome urate_scaled
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Scaled 2004-6 Unemployment Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome prate_scaled
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Scaled 2000 Poverty Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome hs_scaled
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Scaled 2000 HS Grad. Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome coll_scaled
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Scaled 2000 College Grad. Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
replace income2009=income2009/1000
global outcome income2009
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2005 Market Income ($1000s)") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome prate2009
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2000 Poverty Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome urate2009
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("20004-6 Unemployment Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
global outcome hsgradrate
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2000 HS Graduation Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome bachrate
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2000 College Graduation Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome erate2006
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2006 Employment Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
global outcome d_2party2000
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2000 Al Gore Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
global outcome dhouse_2party2004
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2004 House Dem Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
global outcome d_2party2004
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("2004 John Kerry Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_income_scaled" "$dir/figures/bw_prate_scaled" ///
"$dir/figures/bw_income2009" "$dir/figures/bw_prate2009", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_income_scaled")
graph export "$dir/figures/balancecheck_bw1.png",replace


grc1leg "$dir/figures/bw_urate_scaled" "$dir/figures/bw_coll_scaled" ///
"$dir/figures/bw_urate2009" "$dir/figures/bw_bachrate", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_urate_scaled")
graph export "$dir/figures/balancecheck_bw2.png",replace


grc1leg "$dir/figures/bw_hs_scaled" "$dir/figures/bw_d_2party2004" ///
 "$dir/figures/bw_erate2006" "$dir/figures/bw_hsgradrate" ///
"$dir/figures/bw_d_2party2000" "$dir/figures/bw_dhouse_2party2004", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_hs_scaled")
graph export "$dir/figures/balancecheck_bw3.png",replace



}
*Predicted Vote Graphs
{
	
global full hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 i.electionyear i.state i.DMA

preserve
reg d_2party $full
predict predicteddem
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome predicteddem
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Predicted Presidential Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (20) $band )  ylabel(0(0.1)1)  saving("$dir/figures/$outcome",replace)
restore

preserve
reg dhouse_2party $full
predict predictedhousedem
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome predictedhousedem
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Predicted House Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (20) $band )  ylabel(0(0.1)1)  saving("$dir/figures/$outcome",replace)
restore


graph combine "$dir/figures/predicteddem" "$dir/figures/predictedhousedem" , ///
plotregion(color(white)) graphregion(color(white))  
graph export "$dir/figures/predictedvote.png",replace

}
*Estimates
{
	est clear
preserve
label var d_2party2004 "John Kerry 2004 Vote [0-1]" 
label var d_2party2000 "Al Gore 2000 Vote [0-1]" 
label var urate2009 "Unemployment [0-100]" 
label var prate2009 "Poverty Rate [0-100]" 
label var income2009 "Market Income [Dollars]" 
label var hsgradrate "High School [0-100]" 
label var bachrate "College [0-100]"
label var urate_scaled "Scaled Unemployment" 
label var prate_scaled "Scaled Poverty" 
label var income_scaled "Scaled Market Income" 
label var hs_scaled "Scaled High School" 
label var coll_scaled "Scaled College" 
label var erate2006 "Employment Rate [0-1]" 
label var dhouse_2party2004 "2004 House Dem. Vote [0-1]" 

foreach var of varlist  d_2party2004 d_2party2000 ///
  urate2009 prate2009 income2009 hsgradrate bachrate urate_scaled prate_scaled income_scaled  ///
hs_scaled coll_scaled erate2006 dhouse_2party2004 {
	
		

global variable `var'

local varlabel : var label `var'


eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'

esttab $variable  ///
using "$dir/tables/$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "`varlabel'") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 
 
}
restore
}

}
*Graph the Presidential Outcomes
{
preserve
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome pshift
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004 to Post-Reform Swing") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )   saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Post-Reform Democratic Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )  ylabel(0.1(0.1)0.7) saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party2004
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow) mcolor(gs2)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2004 John Kerry Vote") xline(0, lcolor(gs12) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )  ylabel(0.1(0.1)0.7)  saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party2000
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow) mcolor(gs2)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2000 Al Gore Vote") xline(0, lcolor(gs12) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )  ylabel(0.1(0.1)0.7)  saving("$dir/figures/$outcome",replace)
restore

graph combine "$dir/figures/d_2party2000" "$dir/figures/d_2party" "$dir/figures/d_2party2004"  ///
 "$dir/figures/pshift", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/prez.png",replace

}
*Produce Estimates for Presidential Outcomes
{
*Main Static Estimates
{
est clear

global full hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 i.electionyear i.state i.DMA
global partial d_2party2004 dhouse_2party2004 

gen swingstatep=d_2party if (State== "Georgia"| State== "Ohio" | State== "North Carolina"| State== "Pennsylvania"| State== "Virginia") 
gen safestatep=d_2party if (State!= "Georgia" & State!= "Ohio" & State!= "North Carolina" & State!= "Pennsylvania" & State!= "Virginia") 
gen olddemp=d_2party if d_2party2000>0.41
gen oldgopp=d_2party if d_2party2000<0.41


preserve

label var d_2party "Pooled Dem. Vote"
label var d_2party2008 "Obama 2008 Vote"
label var d_2party2012 "Obama 2012 Vote"
label var d_2party2016 "Clinton 2016 Vote"
label var swingstatep "Swing State"
label var safestatep "Safe State"
label var olddemp "More Democratic"
label var oldgopp "More Republican"

foreach var of varlist d_2party d_2party2008 d_2party2012 d_2party2016 swingstatep safestatep olddemp oldgopp   {

global variable `var'

local varlabel : var label `var'


eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009  tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009 $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009  tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdyes


eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localyes



esttab `var'firstno `var'firstyes `var'secondno `var'secondyes ///
`var'localno `var'localyes  ///
using "$dir/tables/$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "`varlabel'") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 }
 
 

restore
 
}

}
*Graph the House of Representatives Outcomes
{

preserve
keep if dhouse_2party!=1 & dhouse_2party!=0
gen bw=2.5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome dhouse_2party
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Post-Reform House Dem. Vote") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )  ylabel(0.1(0.1)0.7)  saving("$dir/figures/$outcome",replace)
restore
graph export "$dir/figures/$outcome.png",replace



}
*Produce Estimates for House of Representatives Outcomes
{
est clear
global full hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 i.electionyear i.state i.DMA
global partial d_2party2004 dhouse_2party2004 

preserve



gen olddemh=dhouse_2party if d_2party2000>0.41
gen oldgoph=dhouse_2party if d_2party2000<0.41

label var dhouse_2party "Pooled Dem. Vote"
label var olddemh "More Democratic"
label var oldgoph "More Republican"



foreach var of varlist dhouse_2party  olddemh oldgoph  {

global variable `var'

local varlabel : var label `var'


eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'firstno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'firstyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'secondno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'secondyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'thirdno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'thirdyes


eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'localno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store `var'localyes


esttab `var'firstno `var'firstyes `var'secondno `var'secondyes ///
`var'localno `var'localyes  ///
using "$dir/tables/$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "`varlabel'") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
}

restore
est clear


}
*Dynamic Estimates
{
est clear

quietly reg d_2party tag2009 tag2009#c.index2009 index2009 distressed distressed#c.index index $partial if (abs(index2009)<=50 | abs(index)<=50) & electionyear==2012 , r cluster(fipscode)
quietly estimates store prez12partial
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "--", replace
estadd local fe "--", replace
estadd local band "50", replace

quietly reg d_2party tag2009 tag2009#c.index2009 index2009 distressed distressed#c.index index $full if (abs(index2009)<=50 | abs(index)<=50) & electionyear==2012 , r cluster(fipscode)
quietly estimates store prez12full
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "Yes", replace
estadd local fe "Yes", replace
estadd local band "50", replace

reg d_2party tag2009 tag2009#c.index2009 index2009 distressed distressed#c.index index $partial if (abs(index2009)<=50 | abs(index)<=50) & electionyear==2016 , r cluster(fipscode)
quietly estimates store prez16partial
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "--", replace
estadd local fe "--", replace
estadd local band "50", replace

reg d_2party tag2009 tag2009#c.index2009 index2009 distressed distressed#c.index index $full if (abs(index2009)<=50 | abs(index)<=50) & electionyear==2016 , r cluster(fipscode)
quietly estimates store prez16full
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "Yes", replace
estadd local fe "Yes", replace
estadd local band "50", replace

reg dhouse_2party tag2009 tag2009#c.index2009 c.index2009 distressed distressed#c.index index  $partial if (abs(index2009)<=50 | abs(index)<=50) & abs(dhouse_2party-0.5)< 0.5 & electionyear!=2008, r cluster(fipscode)
quietly estimates store houseallpartial
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "--", replace
estadd local fe "--", replace
estadd local band "50", replace

reg dhouse_2party tag2009 tag2009#c.index2009 c.index2009 distressed distressed#c.index index  $full if (abs(index2009)<=50 | abs(index)<=50) & abs(dhouse_2party-0.5)< 0.5 & electionyear !=2008, r cluster(fipscode)
quietly estimates store houseallfull
estadd local poly "1", replace
estadd local vote "Yes", replace
estadd local demo "Yes", replace
estadd local fe "Yes", replace
estadd local band "50", replace

esttab prez12partial prez12full prez16partial prez16full houseallpartial houseallfull  ///
using "$dir/tables/timevariation.tex", keep(tag2009 distressed)  nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "2008 Distress" distressed "Reecent Distress") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
}
*Robustness Tests
{
*Alternative Bandwidth Test
{
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(gs7)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(gs7)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Effect on Pooled Democratic Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party",replace)
graph use "$dir/figures/bw_d_2party"
graph export "$dir/figures/bw_d_2party.png",replace
capture drop reg_nobs-b_coeff
restore

forvalues h=2008(4)2016 {
global time `h'
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party`h' tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(gs7)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(gs7)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Effect on $time Democratic Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party$time",replace)
graph use "$dir/figures/bw_d_2party$time"
graph export "$dir/figures/bw_d_2party$time.png",replace
capture drop reg_nobs-b_coeff
restore
}
grc1leg "$dir/figures/bw_d_2party" "$dir/figures/bw_d_2party2008" ///
"$dir/figures/bw_d_2party2012" "$dir/figures/bw_d_2party2016", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party")
graph export "$dir/figures/full_bw.png",replace


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg dhouse_2party tag2009 tag2009#c.index2009 index2009 $full if abs(dhouse_2party-0.5)< 0.5 & abs(index2009)<= `i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(gs7)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(gs7)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(gs7)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Effect on Pooled Democratic Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(5)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_dhouse_2party",replace)
graph use "$dir/figures/bw_dhouse_2party"
graph export "$dir/figures/bw_dhouse_2party.png",replace
capture drop reg_nobs-b_coeff
restore
}
*Alternative Bandwidths with Alternative Specifications
{
*Presidential Elections
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party_full1",replace)
graph use "$dir/figures/bw_d_2party_full1"
graph export "$dir/figures/bw_d_2party_full1.png",replace
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party_partial1",replace)
graph use "$dir/figures/bw_d_2party_partial1"
graph export "$dir/figures/bw_d_2party_partial1.png",replace
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party_full2",replace)
graph use "$dir/figures/bw_d_2party_full2"
graph export "$dir/figures/bw_d_2party_full2.png",replace
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(index2009)<=`i' , r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party_partial2",replace)
graph use "$dir/figures/bw_d_2party_partial2"
graph export "$dir/figures/bw_d_2party_partial2.png",replace
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party_full1" "$dir/figures/bw_d_2party_partial1" ///
"$dir/figures/bw_d_2party_full2" "$dir/figures/bw_d_2party_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party_full1")
graph export "$dir/figures/bw_d_2party_allspecs.png",replace


*House of Representatives elections
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg dhouse_2party tag2009 tag2009#c.index2009 index2009 $full if abs(dhouse_2party-0.5)< 0.5 & abs(index2009)<= `i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_dhouse_2party_full1",replace)
graph use "$dir/figures/bw_dhouse_2party_full1"
graph export "$dir/figures/bw_dhouse_2party_full1.png",replace
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg dhouse_2party tag2009 tag2009#c.index2009 index2009 $partial if abs(dhouse_2party-0.5)< 0.5 & abs(index2009)<= `i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_dhouse_2party_partial1",replace)
graph use "$dir/figures/bw_dhouse_2party_partial1"
graph export "$dir/figures/bw_dhouse_2party_partial1.png",replace
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg dhouse_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(dhouse_2party-0.5)< 0.5 & abs(index2009)<= `i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_dhouse_2party_full2",replace)
graph use "$dir/figures/bw_dhouse_2party_full2"
graph export "$dir/figures/bw_dhouse_2party_full2.png",replace
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg dhouse_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(dhouse_2party-0.5)< 0.5 & abs(index2009)<= `i' , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_dhouse_2party_partial2",replace)
graph use "$dir/figures/bw_dhouse_2party_partial2"
graph export "$dir/figures/bw_dhouse_2party_partial2.png",replace
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_dhouse_2party_full1" "$dir/figures/bw_dhouse_2party_partial1" ///
"$dir/figures/bw_dhouse_2party_full2" "$dir/figures/bw_dhouse_2party_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_dhouse_2party_full1")
graph export "$dir/figures/bw_dhouse_2party_allspecs.png",replace


}
*Alternative Thresholds Test
{
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party tag2009 index2009 tag2009#c.index2009   $full if abs(index2009)<=50 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
quietly reg  d_2party2008 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly replace se_coeff2008= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2008= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2008= abs(b_coeff2008/se_coeff2008) if bandwidth==`i'
quietly reg  d_2party2012 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly replace se_coeff2012= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2012= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2012= abs(b_coeff2012/se_coeff2012) if bandwidth==`i'
quietly reg  d_2party2016 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=50 , r cluster(fipscode)
quietly replace se_coeff2016= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2016= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2016= abs(b_coeff2016/se_coeff2016) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(gs7)) ///
(line t_stat2012 bandwidth, lcolor(gs10)) (line t_stat2016 bandwidth, lcolor(gs13)) if bandwidth >= -50 & bandwidth<= 30, ///
 ytitle("t-Statistic for RD Model at Given Rank Index Value") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(order(1 "Pooled" 2 "2008" 3 "2012"  4 "2016" ) symxsize(10) col(4)) saving("$dir/figures/synth_d_2party",replace) 
graph use "$dir/figures/synth_d_2party"
graph export "$dir/figures/synth_d_2party.png",replace
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  dhouse_2party tag2009 index2009 tag2009#c.index2009   ///
 $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.45, r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
 ytitle("t-Statistic for RD Model at Given Rank Index Value") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_dhouse_2party",replace) 
graph use "$dir/figures/synth_dhouse_2party"
graph export "$dir/figures/synth_dhouse_2party.png",replace
collapse (mean) t_stat, by(bandwidth)
capture drop reg_nobs-b_coeff
restore

}
*2004 Falsification Tests
{
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2004 tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Effect on John Kerry 2004 Vote") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(7) col(3)) yscale(titlegap(small)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2004",replace)
capture drop reg_nobs-b_coeff
restore



preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party2004 tag2009 index2009 tag2009#c.index2009   if abs(index2009)<=50 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
 ytitle("t-Statistic for RD Model at Given Rank Index Value") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_d_2party2004",replace) 
collapse (mean) t_stat, by(bandwidth)
capture drop reg_nobs-b_coeff
restore

graph combine "$dir/figures/bw_d_2party2004" "$dir/figures/synth_d_2party2004", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/falsification_d_2party2004.png",replace

}
}
*Null Mechanisms Graphs
{
*Background Section
{
preserve
gen rbin= 2.5*floor(rindex/2.5)+1.25
gen count=1
collapse (sum) count (mean) truematch generosity2009 LDDgenerosity2009 ADHSgenerosity2009 Roadgenerosity2009 rindex,  by(rbin) 

twoway  (scatter generosity rbin , mcolor(blue)  msymbol(triangle_hollow) msize(medlarge)) ///
		(scatter LDDgenerosity rbin , mcolor(red)   msymbol(square_hollow) msize(medlarge)) ///
		(scatter Roadgenerosity rbin , mcolor(green)  msymbol(circle_hollow) msize(medlarge)) ///
		(scatter truematch rbin, msize(tiny) msymbol(circle) mcolor(black))  ///
		if rindex>=0, legend(label(1 "Max for General Grants") label(2 "Max for LDD Grants") label(3 "Max for Rural Roads") ///
		label(4 "Actual Match Rate") label(5 "Fitted Local Polynomial") cols(2) symxsize(5))  ///
		xtitle("County Distress Index (Percentile Rank)") ytitle("Grant Generosity") xline(90, lcolor(gray) lpattern(dash)) ///
		xline(75, lcolor(gray) lpattern(dash)) xline(25, lcolor(gray) lpattern(dash)) xline(10, lcolor(gray) lpattern(dash)) ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( 0(10)100 ) ylabel(0(0.1)1) saving("$dir/figures/generosity",replace)
graph use "$dir/figures/generosity"
graph export "$dir/figures/generosity.png",replace
restore

}
*Main Graphs
{
preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome totfunding_percap
reg $outcome tag2009##c.index2009     if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("ARC Spending Per Capita") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band )  saving("$dir/figures/$outcome",replace)
restore



preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome truematch
reg $outcome tag2009##c.index2009    if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("ARC Federal Match Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band )  saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome erate
reg $outcome tag2009##c.index2009    if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Prime Age Employment Rate") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band )  saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome arcsearch
reg $outcome tag2009##c.index2009    if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("ARC Awareness Index") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band )  saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome federalexp_percap
reg $outcome tag2009##c.index2009    if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("All Federal Grant Spending") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band ) saving("$dir/figures/$outcome",replace)
restore


preserve
gen bw=5
global band 100
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome fns_waived
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("SNAP Work Waiver Leniency") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (50) $band ) saving("$dir/figures/$outcome",replace)
restore



graph combine "$dir/figures/erate" "$dir/figures/totfunding_percap" "$dir/figures/truematch"  ///
 "$dir/figures/arcsearch" "$dir/figures/federalexp_percap" "$dir/figures/fns_waived", ///
plotregion(color(white) ) graphregion(color(white) )
graph export "$dir/figures/mechanisms.png",replace
}
*Robustness to Bandwidth
{

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome erate
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Employment Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome totfunding_percap
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009  if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Total ARC Spending Per Cap.") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome truematch
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009  if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("True ARC Matching Rate") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome arcsearch
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009  if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("ARC Awareness Index") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome federalexp_percap
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009  if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("All Federal Grants Per Cap.") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
graph use "$dir/figures/bw_$outcome"
graph export "$dir/figures/bw_$outcome.png",replace
capture drop reg_nobs-b_coeff
restore



preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome fns_waived
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009  if abs(index2009)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("SNAP Work Req. Waiver") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
graph use "$dir/figures/bw_$outcome"
graph export "$dir/figures/bw_$outcome.png",replace
capture drop reg_nobs-b_coeff
restore


grc1leg "$dir/figures/bw_erate" "$dir/figures/bw_totfunding_percap" "$dir/figures/bw_truematch" ///
 "$dir/figures/bw_arcsearch" "$dir/figures/bw_federalexp_percap"  "$dir/figures/bw_fns_waived", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_totfunding_percap")
graph export "$dir/figures/bw_mechanisms.png",replace


}
}
*Produce Estimates for Mechanisms 
{
*Null Mechanisms
{
quietly reg erate tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store erate1

quietly reg totfunding_percap tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store totfunding_percap

quietly reg truematch tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store truematch

quietly reg arcsearch tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store arcsearch

quietly reg federalexp_percap tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store federalexp_percap

quietly reg fns_waived tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=50, r cluster(fipscode)
estimates store fns_waived


 esttab erate1 totfunding_percap truematch arcsearch federalexp_percap fns_waived ///
using "$dir/tables/mechanisms.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps  ///
coeflabel(tag2009 "Distressed") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 }


 
}


*Editor Comments
{
	
pca ElevFt DTVsignal total_prov
	
	
preserve
gen media2=((DTVsignal-4.264019 )/4.375579 )+((total_prov-11.38084 )/5.223391) -((ElevFt-2343.418 )/1406.406)
replace media2=media2/2.099923
gen interact2= tag2009*c.media2
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media2_1partial50
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 index2009  $full if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media2_1full50
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media2_2partial50
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media2_2full50
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 index2009  $partial if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2_1partial25
eststo: quietly reg d_2party  tag2009 interact2 media2 tag2009#c.index2009 index2009  $full if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2_1full25

esttab media2_1partial50 media2_1full50 media2_2partial50 media2_2full50 media2_1partial25 media2_1full25 ///
using "$dir/tables/media2_prez.tex", keep(tag2009 interact2 media2)  nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Distress" interact2 "Distress $\times$ Media" media2 "Media") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore

est clear

eststo: quietly reg d_2party2000  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2000
eststo: quietly reg d_2party2004  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2004
eststo: quietly reg d_2party2008  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2008
eststo: quietly reg d_2party2012  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2012
eststo: quietly reg d_2party2016  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media2016
eststo: quietly reg d_2party  c.media if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store mediapost
esttab media2000 media2004 media2008 media2012 media2016 mediapost   ///
using "$dir/tables/media_time.tex", keep(media) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(media "Media") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 
est clear 
 


 preserve
 generate interaction=c.media*tag2009
 foreach var of varlist hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 density over65  {

global variable `var'

eststo: quietly reg `var'  tag2009 interaction media tag2009#c.index2009 index2009 if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store `var'interact

 }

esttab hs_scaledinteract coll_scaledinteract urate_scaled2009interact prate_scaled2009interact income_scaled2009interact densityinteract over65interact ///
using "$dir/tables/interact_balance.tex", keep(tag2009 interaction media) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Distress" interaction "Distress $\times$ Media" media "Media") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore



preserve
generate interaction=c.media*tag2009
est clear
foreach var of varlist density over65 hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 d_2party2000  {
eststo: quietly reg d_2party  i.tag2009 interaction c.media i.tag2009##c.`var' tag2009#c.index2009 index2009  $partial if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store `var'25partial
}
esttab hs_scaled25partial coll_scaled25partial urate_scaled200925partial prate_scaled200925partial income_scaled200925partial density25partial over6525partial ///
using "$dir/tables/compete_25partial.tex", keep(interaction) nolines fragment nomtitles nonumbers nogaps ///
coeflabel( interaction "Distress $\times$ Media" ) ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore

preserve
generate interaction=c.media*tag2009
est clear
foreach var of varlist density over65 hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 d_2party2000  {
eststo: quietly reg d_2party  i.tag2009 interaction c.media i.tag2009##c.`var' tag2009#c.index2009 index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store `var'50partial
}
esttab hs_scaled50partial coll_scaled50partial urate_scaled200950partial prate_scaled200950partial income_scaled200950partial density50partial over6550partial ///
using "$dir/tables/compete_50partial.tex", keep(interaction) nolines fragment nomtitles nonumbers nogaps ///
coeflabel( interaction "Distress $\times$ Media" ) ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore

preserve
generate interaction=c.media*tag2009
est clear
foreach var of varlist density over65 hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 d_2party2000  {
eststo: quietly reg d_2party  i.tag2009 interaction c.media i.tag2009##c.`var' tag2009#c.index2009 index2009  $full if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store `var'25full
}
esttab hs_scaled25full coll_scaled25full urate_scaled200925full prate_scaled200925full income_scaled200925full density25full over6525full ///
using "$dir/tables/compete_25full.tex", keep(interaction) nolines fragment nomtitles nonumbers nogaps ///
coeflabel( interaction "Distress $\times$ Media" ) ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore

preserve
generate interaction=c.media*tag2009
est clear
foreach var of varlist density over65 hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 d_2party2000  {
eststo: quietly reg d_2party  i.tag2009 interaction c.media i.tag2009##c.`var' tag2009#c.index2009 index2009  $full if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store `var'50full
}
esttab hs_scaled50full coll_scaled50full urate_scaled200950full prate_scaled200950full income_scaled200950full density50full over6550full ///
using "$dir/tables/compete_50full.tex", keep(interaction) nolines fragment nomtitles nonumbers nogaps ///
coeflabel( interaction "Distress $\times$ Media" ) ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore


}

*Reviewer 1 Edits
{
est clear

*Point 2
{
global placebo_full hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009  i.electionyear i.state i.DMA d_2party1996
global placcebo_partial d_2party1996 

preserve

label var d_2party2000 "Gore 2000 Vote"
label var d_2party2004 "Kerry 2004 Vote"


foreach var of varlist d_2party2000 d_2party2004  {


global variable `var'

local varlabel : var label `var'


eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $placebo_partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $placebo_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009  tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009 $placebo_partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009  tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009 $placebo_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdyes


eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $placebo_partial if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 index2009 $placebo_full if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localyes



esttab `var'firstno `var'firstyes `var'secondno `var'secondyes ///
`var'localno `var'localyes  ///
using "$dir/tables/placebo_$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "`varlabel'") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 }
 
 
restore
 
 
 }
 
 

*Point 5
{
 *Presidential Elections 2008
 {
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' & electionyear==2008, r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2008_full1",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=`i' & electionyear==2008, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2008_partial1",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(index2009)<=`i' & electionyear==2008, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2008_full2",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(index2009)<=`i' & electionyear==2008, r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2008_partial2",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party2008_full1" "$dir/figures/bw_d_2party2008_partial1" ///
"$dir/figures/bw_d_2party2008_full2" "$dir/figures/bw_d_2party2008_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party2008_full1")
graph export "$dir/figures/bw_d_2party2008_allspecs.png",replace

}


*Presidential Elections 2012
 {
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' & electionyear==2012, r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2012_full1",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=`i' & electionyear==2012, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2012_partial1",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(index2009)<=`i' & electionyear==2012, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2012_full2",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(index2009)<=`i' & electionyear==2012, r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2012_partial2",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party2012_full1" "$dir/figures/bw_d_2party2012_partial1" ///
"$dir/figures/bw_d_2party2012_full2" "$dir/figures/bw_d_2party2012_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party2012_full1")
graph export "$dir/figures/bw_d_2party2012_allspecs.png",replace

}


*Presidential Elections 2016
 {
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $full if abs(index2009)<=`i' & electionyear==2016, r cluster(fipscode)    
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2016_full1",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 index2009 $partial if abs(index2009)<=`i' & electionyear==2016, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2016_partial1",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $full if abs(index2009)<=`i' & electionyear==2016, r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2016_full2",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $partial if abs(index2009)<=`i' & electionyear==2016, r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2016_partial2",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party2016_full1" "$dir/figures/bw_d_2party2016_partial1" ///
"$dir/figures/bw_d_2party2016_full2" "$dir/figures/bw_d_2party2016_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party2016_full1")
graph export "$dir/figures/bw_d_2party2016_allspecs.png",replace

}


 *Presidential Elections 2000
 {
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2000 tag2009 tag2009#c.index2009 index2009 $placebo_full if abs(index2009)<=`i' , r cluster(fipscode)    
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2000_full1",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2000 tag2009 tag2009#c.index2009 index2009 $placebo_partial if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2000_partial1",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2000 tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_full if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2000_full2",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2000 tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_partial if abs(index2009)<=`i' , r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.1(0.1)0.3) saving("$dir/figures/bw_d_2party2000_partial2",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party2000_full1" "$dir/figures/bw_d_2party2000_partial1" ///
"$dir/figures/bw_d_2party2000_full2" "$dir/figures/bw_d_2party2000_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party2000_full1")
graph export "$dir/figures/bw_d_2party2000_allspecs.png",replace

}


 *Presidential Elections 2004
 {
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2004 tag2009 tag2009#c.index2009 index2009 $placebo_full if abs(index2009)<=`i' , r cluster(fipscode)    
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2004_full1",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2004 tag2009 tag2009#c.index2009 index2009 $placebo_partial if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Local Linear Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2004_partial1",replace)
capture drop reg_nobs-b_coeff
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2004 tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_full if abs(index2009)<=`i' , r cluster(fipscode) 
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic with Covariates") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.05(0.05)0.15) saving("$dir/figures/bw_d_2party2004_full2",replace)
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg d_2party2004 tag2009 tag2009#c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 $placebo_partial if abs(index2009)<=`i' , r cluster(fipscode)  
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=100, ///
 ytitle("Quadratic Base Model") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(-0.1(0.1)0.3) saving("$dir/figures/bw_d_2party2004_partial2",replace)
capture drop reg_nobs-b_coeff
restore

grc1leg "$dir/figures/bw_d_2party2004_full1" "$dir/figures/bw_d_2party2004_partial1" ///
"$dir/figures/bw_d_2party2004_full2" "$dir/figures/bw_d_2party2004_partial2", ///
plotregion(color(white)) graphregion(color(white))  legendfrom("$dir/figures/bw_d_2party2004_full1")
graph export "$dir/figures/bw_d_2party2004_allspecs.png",replace

}

}

*Point 6
{
preserve
gen swing0408=d_2party2008-d_2party2004
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome swing0408
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow) mcolor(gs2)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Kerry 2004 to Obama 2008 Swing") xline(0, lcolor(gs12) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )   saving("$dir/figures/$outcome",replace)
restore

preserve
gen bw=5
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome d_2party2008
reg $outcome tag2009##c.index2009  if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow) mcolor(gs2)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("2008 Barack Obama Vote") xline(0, lcolor(gs12) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (10) $band )  ylabel(0.1(0.1)0.7) saving("$dir/figures/$outcome",replace)
restore

graph combine "$dir/figures/d_2party2000" "$dir/figures/d_2party2008" "$dir/figures/d_2party2004"  ///
 "$dir/figures/swing0408", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/prez_08.png",replace


}

*Point 7
{
preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party tag2009 index2009 tag2009#c.index2009   $full if abs(index2009)<=25 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
quietly reg  d_2party2008 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=25 , r cluster(fipscode)
quietly replace se_coeff2008= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2008= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2008= abs(b_coeff2008/se_coeff2008) if bandwidth==`i'
quietly reg  d_2party2012 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=25 , r cluster(fipscode)
quietly replace se_coeff2012= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2012= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2012= abs(b_coeff2012/se_coeff2012) if bandwidth==`i'
quietly reg  d_2party2016 tag2009 index2009 tag2009#c.index2009 $full if abs(index2009)<=25 , r cluster(fipscode)
quietly replace se_coeff2016= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff2016= _b[tag2009] if bandwidth==`i'
quietly replace t_stat2016= abs(b_coeff2016/se_coeff2016) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >= -50 & bandwidth<= 30, ///
 ytitle("t-Statistic for RD Model at Given Rank Index Value") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(order(1 "Pooled" 2 "2008" 3 "2012"  4 "2016" ) symxsize(10) col(4)) saving("$dir/figures/synth_d_2party_25pt",replace) 
graph use "$dir/figures/synth_d_2party_25pt"
graph export "$dir/figures/synth_d_2party_25pt.png",replace

collapse (mean) t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
quietly sum t_stat if bandwidth==0
local actual=r(mean)
quietly sum t_stat2008 if bandwidth==0
local actual2008=r(mean)
quietly sum t_stat2012 if bandwidth==0
local actual2012=r(mean)
quietly sum t_stat2016 if bandwidth==0
local actual2016=r(mean)

cdfplot t_stat , title("Pooled", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
 saving("$dir/figures/falsif_pooled",replace) nodraw
cdfplot t_stat2008 , title("2008", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual2008', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
 saving("$dir/figures/falsif_2008",replace) nodraw
 cdfplot t_stat2012 , title("2012", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual2012', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
 saving("$dir/figures/falsif_2012",replace) nodraw
 cdfplot t_stat2016 , title("2016", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual2016', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
 saving("$dir/figures/falsif_2016",replace) nodraw
 
graph combine "$dir/figures/falsif_pooled" "$dir/figures/falsif_2008" ///
"$dir/figures/falsif_2012" "$dir/figures/falsif_2016", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/falsif.png",replace

capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party2004 tag2009 index2009 tag2009#c.index2009 $placebo_full  if abs(index2009)<=25 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
title("Kerry 2004 with 25 Point Bandwidth", size(small)) ytitle("t-Statistic for RD Model") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_d_2party2004_25pt",replace) 
collapse (mean) t_stat, by(bandwidth)

quietly sum t_stat if bandwidth==0
local actual=r(mean)
cdfplot t_stat , title("Kerry 2004 with 25 Point Bandwidth", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
saving("$dir/figures/falsif_2004narrow",replace) nodraw
 
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party2000 tag2009 index2009 tag2009#c.index2009 $placebo_full  if abs(index2009)<=25 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
title("Gore 2000 with 25 Point Bandwidth", size(small)) ytitle("t-Statistic for RD Model") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_d_2party2000_25pt",replace) 
collapse (mean) t_stat, by(bandwidth)

quietly sum t_stat if bandwidth==0
local actual=r(mean)
cdfplot t_stat , title("Gore 2000 with 25 Point Bandwidth", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
saving("$dir/figures/falsif_2000narrow",replace) nodraw

capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party2004 tag2009 index2009 tag2009#c.index2009 $placebo_full  if abs(index2009)<=50 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
title("Kerry 2004 with 50 Point Bandwidth", size(small)) ytitle("t-Statistic for RD Model") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_d_2party2004",replace) 
collapse (mean) t_stat, by(bandwidth)

quietly sum t_stat if bandwidth==0
local actual=r(mean)
cdfplot t_stat , title("Kerry 2004 with 50 Point Bandwidth", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
saving("$dir/figures/falsif_2004wide",replace) nodraw
 
capture drop reg_nobs-b_coeff
restore


preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen t_stat=.
gen t_stat2008=.
gen t_stat2012=.
gen t_stat2016=.
gen se_coeff2008=.
gen se_coeff2012=.
gen se_coeff2016=.
gen b_coeff2008=.
gen b_coeff2012=.
gen b_coeff2016=.
gen b_coeff=.
gen bandwidth=abs(1*int(compositeindex2009/1))
forvalues i = 120(1)200 {
quietly replace tag2009=0
quietly replace tag2009=1 if compositeindex2009>=`i'
quietly replace index2009=compositeindex2009-`i'
quietly reg  d_2party2000 tag2009 index2009 tag2009#c.index2009 $placebo_full  if abs(index2009)<=50 , r cluster(fipscode)
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
quietly replace t_stat= abs(b_coeff/se_coeff) if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs t_stat t_stat2008 t_stat2012 t_stat2016, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
replace bandwidth=bandwidth-170
twoway  (line t_stat bandwidth, lcolor(black)) (line t_stat2008 bandwidth, lcolor(blue)) ///
(line t_stat2012 bandwidth, lcolor(red)) (line t_stat2016 bandwidth, lcolor(green)) if bandwidth >=-50 & bandwidth<=30, ///
title("Gore 2000 with 50 Point Bandwidth", size(small)) ytitle("t-Statistic for RD Model") xtitle("Rank Index Value Relative to True Cutoff") ylabel(0(1)4) ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(-50(10)30) ///
legend(off)  saving("$dir/figures/synth_d_2party2000",replace) 
collapse (mean) t_stat, by(bandwidth)

quietly sum t_stat if bandwidth==0
local actual=r(mean)
cdfplot t_stat , title("Gore 2000 with 50 Point Bandwidth", size(small)) xtitle("t-statistic") ytitle("Share of Estimates below t-Statistic") plotregion(color(white)) graphregion(color(white)) ///
xline( `actual', lcolor(red) lpattern(dash) ) ylabel(0(0.2)0.8 0.9(0.05)1, labsize(vsmall)) ///
saving("$dir/figures/falsif_2000wide",replace) nodraw
 
capture drop reg_nobs-b_coeff
restore

graph combine "$dir/figures/synth_d_2party2000_25pt" "$dir/figures/synth_d_2party2000" ///
"$dir/figures/synth_d_2party2004_25pt" "$dir/figures/synth_d_2party2004", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/synth_placebo.png",replace

graph combine "$dir/figures/falsif_2000narrow" "$dir/figures/falsif_2000wide" ///
"$dir/figures/falsif_2004narrow" "$dir/figures/falsif_2004wide", ///
plotregion(color(white)) graphregion(color(white))
graph export "$dir/figures/falsif_placebo.png",replace



}


}


*Reviewer 2 Edits
{
est clear

*Point 1
{
preserve
global outcome netflow
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
gen bandwidth=abs(1*int(index2009/1))
forvalues i = 20(1)100 {
quietly  reg $outcome tag2009 tag2009#c.index2009 index2009 if abs(index2009)<=`i' , r cluster(fipscode)    
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[tag2009] if bandwidth==`i'
quietly replace b_coeff= _b[tag2009] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Net Migration Flow (2009-2013)") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3))  saving("$dir/figures/bw_$outcome",replace)
capture drop reg_nobs-b_coeff
restore
graph use "$dir/figures/bw_netflow"
graph export "$dir/figures/bw_netflow.png",replace


}

*Point 2 
{

preserve
gen interact= tag2009*c.media

eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media_1partial50
eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 index2009  $full if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media_1full50
eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media_2partial50
eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store media_2full50
eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 index2009  $partial if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media_1partial25
eststo: quietly reg d_2party  tag2009 interact media tag2009#c.index2009 index2009  $full if abs(index2009)<=25, r cluster(fipscode)
quietly estimates store media_1full25

esttab media_1partial50 media_1full50 media_2partial50 media_2full50 media_1partial25 media_1full25 ///
using "$dir/tables/media_prez.tex", keep(tag2009 interact media) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Distress" interact "Distress $\times$ Media" media "Media") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace


eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_1partial50
eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_1full50
eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_2partial50
eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_2full50
eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 index2009  $partial if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_1partial25
eststo: quietly reg dhouse_2party  tag2009 interact media tag2009#c.index2009 index2009  $full if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store media_1full25

esttab media_1partial50 media_1full50 media_2partial50 media_2full50 media_1partial25 media_1full25 ///
using "$dir/tables/media_house.tex", keep(tag2009 interact media)  nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Distress" interact "Distress $\times$ Media" media "Media") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
restore
 

}

*Point 3
{

est clear
*Point 3a
{

preserve

gen interact= tag2009*c.d_2party2000



eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partyfirstno

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partyfirstyes

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partysecondno

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partysecondyes

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partythirdno

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $full if abs(index2009)<=50 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partythirdyes

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partylocalno

eststo: quietly reg dhouse_2party tag2009 interact d_2party2000 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=25 & abs(dhouse_2party-0.5)< 0.5, r cluster(fipscode)
quietly estimates store dhouse_2partylocalyes


esttab dhouse_2partyfirstno dhouse_2partyfirstyes dhouse_2partysecondno dhouse_2partysecondyes ///
dhouse_2partylocalno dhouse_2partylocalyes  ///
using "$dir/tables/reviewer2_ancestral.tex", keep(tag2009 interact)  nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Distress" interact "Distress $\times$ Ancestral") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace

restore
}

est clear
*Point 3b
{


preserve


eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partyfirstno

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=50  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partyfirstyes

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partysecondno

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partysecondyes

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partythirdno

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $full if abs(index2009)<=50  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partythirdyes

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partylocalno

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=25  & tippingpoint>0, r cluster(fipscode)
quietly estimates store d_2partylocalyes


esttab d_2partyfirstno d_2partyfirstyes d_2partysecondno d_2partysecondyes ///
d_2partylocalno d_2partylocalyes  ///
using "$dir/tables/reviewer2_tippingpoint2yes.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "538 Swing State") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 
 eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partyfirstno

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=50  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partyfirstyes

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partysecondno

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partysecondyes

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partythirdno

eststo: quietly reg d_2party tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $full if abs(index2009)<=50  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partythirdyes

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partylocalno

eststo: quietly reg d_2party tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=25  & tippingpoint==0, r cluster(fipscode)
quietly estimates store d_2partylocalyes


esttab d_2partyfirstno d_2partyfirstyes d_2partysecondno d_2partysecondyes ///
d_2partylocalno d_2partylocalyes  ///
using "$dir/tables/reviewer2_tippingpoint2no.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "538 Safe State") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace

restore

}


}

est clear
*Point 5
{



preserve

gen bw=2
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome futtag
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0  , lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("2008 Normalized Rank Index") ytitle("Distressed % of Post-2008 Cycles") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

preserve

gen bw=2
global band 50
gen rbin= bw*floor(index2009/bw)+bw/2
gen count=1
global outcome tag2011
reg $outcome tag2009##c.index2009 if abs(index2009)<= $band , r cluster(fipscode)
predict hat
keep if abs(index2009)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("2008 Normalized Rank Index") ytitle("2010 Distress") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (25) $band ) saving("$dir/figures/$outcome",replace)
restore

graph combine "$dir/figures/futtag" "$dir/figures/tag2011" , ///
plotregion(color(white)) graphregion(color(white)) row(2)
graph export "$dir/figures/persistence.png",replace


}


est clear
*Point 9
{

label var erate "Employment"
label var totfunding_percap "Total Funding"
label var truematch "Match Rate"
label var federalexp_percap "Total Grants"
label var fns_waived "SNAP Waivers"
label var netflow "Net Migration"


foreach var of varlist erate totfunding_percap truematch federalexp_percap fns_waived netflow {

global variable `var'

local varlabel : var label `var'


eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50, r cluster(fipscode)
quietly estimates store `var'secondno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $full if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localyes


 esttab  `var'firstno `var'firstyes `var'secondno `var'secondyes ///
`var'localno `var'localyes ///
using "$dir/tables/mechanisms_robust_$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "`varlabel'") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace
 

}


global modified_full hs_scaled coll_scaled urate_scaled2009 prate_scaled2009 income_scaled2009 d_2party2004 dhouse_2party2004 i.electionyear i.state 


foreach var of varlist arcsearch  {

global variable `var'

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $modified_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'firstyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009  $modified_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'secondyes

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $partial if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdno

eststo: quietly reg `var' tag2009 tag2009##c.index2009 c.index2009 tag2009#c.index2009#c.index2009 c.index2009#c.index2009 ///
 tag2009#c.index2009#c.index2009#c.index2009 c.index2009#c.index2009#c.index2009  $modified_full if abs(index2009)<=50 , r cluster(fipscode)
quietly estimates store `var'thirdyes

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $partial if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localno

eststo: quietly reg `var' tag2009 tag2009#c.index2009 c.index2009  $modified_full if abs(index2009)<=25 , r cluster(fipscode)
quietly estimates store `var'localyes

 esttab  `var'firstno `var'firstyes `var'secondno `var'secondyes ///
`var'localno `var'localyes ///
using "$dir/tables/mechanisms_robust_$variable.tex", keep(tag2009) nolines fragment nomtitles nonumbers nogaps ///
coeflabel(tag2009 "Media Awareness") ///
b(%9.3f) se ///
title("") ///
 star(+ 0.1 * 0.05 ** 0.01) ///
s(, label()) ///
 replace

}
}


}




*Mechanisms Section: Graphs for Articles
{
clear
use "$dir/News/news.dta"
sum local-grants
codebook source
tab publicnews if local==1
sum distressed-othermention
sum distressedonly distressedmore if countystatus==1


clear
use "$dir/News/media_coverage.dta"

preserve
gen bw=5
global band 100
gen rbin= bw*floor(index/bw)+bw/2
gen count=1
global outcome mentions
reg $outcome distressed##c.index  if abs(index)<= $band , r cluster(fipscode)
predict hat
keep if abs(index)<= $band
collapse (sum) count (mean) $outcome hat ,  by(rbin) 

twoway  (scatter $outcome rbin , msize(vsmall) msymbol(circle_hollow)) ///
		(qfit hat rbin  if rbin<0, lcolor(black)) ///
        (qfit hat rbin  if  rbin>=0,  lcolor(black)) ///
		, legend(off) xtitle("Normalized Rank Index") ytitle("Annual County Mentions and Map Highlights") xline(0, lcolor(red) lpattern(dash))  ///
		 plotregion(color(white)) graphregion(color(white)) xlabel( -$band (20) $band ) saving("$dir/figures/county_tag",replace)
graph use "$dir/figures/county_tag"
graph export "$dir/figures/county_tag.png",replace
restore

preserve
capture drop reg_nobs-b_coeff
gen reg_nobs=.
gen se_coeff=.
gen b_coeff=.
global outcome mentions
gen bandwidth=abs(1*int(index/1))
forvalues i = 20(1)100 {
quietly  reg $outcome distressed distressed#c.index index  if abs(index)<=`i' , r cluster(fipscode)   
quietly replace reg_nobs=e(N) if bandwidth==`i'
quietly replace se_coeff= _se[distressed] if bandwidth==`i'
quietly replace b_coeff= _b[distressed] if bandwidth==`i'
  }
collapse (mean) b_coeff se_coeff reg_nobs, by(bandwidth)
sort bandwidth
gen lowerbound=b_coeff-1.96*se_coeff
gen upperbound=b_coeff+1.96*se_coeff
gen lowerbound2=b_coeff-1.65*se_coeff
gen upperbound2=b_coeff+1.65*se_coeff
twoway (line b_coeff bandwidth, lcolor(black)) (line lowerbound bandwidth, lpattern(dash) lcolor(blue)) ///
 (line upperbound bandwidth, lpattern(dash) lcolor(blue)) ///
(line lowerbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 (line upperbound2 bandwidth, lpattern(dot) lcolor(blue)) ///
 if bandwidth >=20 & bandwidth<=105, ///
 ytitle("Annual County Mentions and Map Highlights") xtitle("Bandwidth") ///
plotregion(color(white)) graphregion(color(white)) yline(0,lcolor(grey) lpattern(dot))  xlabel(20(10)100) ///
legend(order(1 "Estimate" 2 "95% CI" 4 "90% CI") symxsize(10) col(3)) ylabel(0(1)4) saving("$dir/figures/bw_$outcome",replace)
graph use "$dir/figures/bw_$outcome"
graph export "$dir/figures/bw_$outcome.png",replace
capture drop reg_nobs-b_coeff
restore

clear
import excel "$dir/News/AccessWorldNews.xlsx", sheet("Sheet 4") cellrange(A2:C28) firstrow

twoway (scatter ARCArticles Year, msymbol(Oh) mcolor(blue)) (lfit ARCArticles Year if Year<=2007 , lcolor(black)) ///
(lfit ARCArticles Year if Year>=2008 , lcolor(black)) (scatter DRAArticles Year, msymbol(Th) mcolor(gray)), xline(2007.5, lcolor(red) lpattern(dash)) ///
 xtitle("Year") ytitle("Annual Newspaper Articles") plotregion(color(white)) graphregion(color(white)) ///
 legend(order(1 "Appalachian Regional Commission" 4 "Delta Regional Authority") symxsize(5)) saving("$dir/figures/articles",replace)
 graph use "$dir/figures/articles"
graph export "$dir/figures/articles.png",replace

clear
import excel "$dir/News/AccessWorldNews.xlsx", sheet("Sheet 1") cellrange(A2:B54) firstrow clear
tab Location [fw= Articles], sort

clear
import excel "$dir/News/AccessWorldNews.xlsx", sheet("Sheet 2") cellrange(A2:G1013) firstrow clear
split Newspaper, parse(" (") limit(1) gen(newspaper)
drop Newspaper
rename newspaper Newspaper
tab Newspaper [fw= Stories], sort
tab Location [fw= Stories], sort


}




log close




